import $ from './library/jquery.js';
let phone = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/;
let email1 = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
let email = null;
let username = null;
let password = null;
let phonenum = null;
let loginInfo = $('.pass b');
$('.num input').on('change', function() {
    if (!phone.test(this.value)) {
        $('.num em').text("请输入正确的手机号");
        $('.num em').css('color', 'red')
    } else {
        $('.num em').text("手机号正确");
        let string = '';
        for (let i = 0; i < 6; i++) {
            let num = Math.ceil(Math.random() * 26 + 96);
            string += String.fromCharCode(num);
        }
        $(".verText").text(string);
        $('.verify input').on('change', function() {
            if (this.value == string) {
                $('.nextbtn div').css('background-color', 'red')
                $('.nextbtn div').on('click', function() {
                    phonenum = $('.num input').val();
                    $('.part1').addClass('display').siblings('.part2').removeClass('display');
                    $('ul li:eq(0)').removeClass().siblings('li:eq(0)').addClass('active')
                })
            }
        });
    }
});
$('.sec1 input').on("change", function() {
    if (!email1.test(this.value)) {
        $(".sec1 em").text('请输入正确的邮箱号');
        $(".sec1 em").css('color', 'red')
    } else {
        email = this.value;
    }
});
$(".pass input").on('input', function() {
    let reg = [
        /^.{6,}$/, // 验证长度
        /[A-Z]+/,
        /[a-z]+/,
        /\d+/,
        /[^A-Za-z0-9]+/,
    ];
    let check = reg.map(el => el.test(this.value));
    let len = check.shift();
    let strong = check.reduce((prev, next) => prev + next);
    if (len) {
        switch (strong) {
            case 1:
            case 2:
                loginInfo.html('密码强度：弱');
                loginInfo.css('color', 'red');
                break;
            case 3:
                loginInfo.html('密码强度：中');
                loginInfo.css('color', 'yellow');
                break;
            case 4:
                loginInfo.html('密码强度：强');
                loginInfo.css('color', 'green');
                break;
        }

    } else {
        loginInfo.html('密码长度不足6位');
        loginInfo.css('color', 'red');
    }
    password = $(this).val();
});
$('.confirm input').on('change', function() {
    if (this.value !== password) {
        $('.confirm b').html('两次密码不一致');
        $('.comfirm b').css('color', 'red');
    } else {
        $('.sec4  div').on('click', function() {
            if (username != null && username != null && email != null && phonenum != null) {
                $.ajax({
                    url: '../../interface/register.php',
                    type: 'get',
                    datatype: 'json',
                    data: {
                        username,
                        email,
                        phonenum,
                        password
                    }
                }).then(res => {
                    if (res == '注册成功') {
                        $('.part2').addClass('display').siblings('.part3').removeClass('display');
                        $('ul li:eq(1)').removeClass().siblings('li:eq(1)').addClass('active')
                        setTimeout(function() {
                            location.href = "../html/login.html"
                        }, 500);
                    } else {
                        alert(res);
                    }

                }).catch(xhr => {
                    console.log(xhr.status);
                })
            } else {
                alert('请把信息填写完整');
            }
        })
    }
});
$('.sec3 input').on('change', function() {
    username = this.value;
});